<?php
/**
 * Redirector.php
 *
 * @category    Madoqua
 * @package     Redirector
 */

/**
 * Madoqua_Redirector
 *
 * class to perform redirects using the router
 *
 * @category    Madoqua
 * @package     Redirector
 */
class Madoqua_Redirector
{
    /**
     * redirect to a route and halt execution
     *
     * @param array $params
     * @param string $routeName
     * @param bool $reset
     */
    public static function redirectAndExit($params = array(), $routeName = null, $reset = false)
    {
        $frontController = Madoqua_Application::getFront();
        //the front controller

        $router = Madoqua_Application::getFront()->getRouter();
        //the router (assumes rewrite router)

        if ($reset && $routeName === null) {
            $routeName = 'default';
        }
        //hmm... this may lead to unexpected results
        //but so will getting the current route name

        if ($routeName == null) {
            try {
                $routeName = $router->getCurrentRouteName();
            } catch (Zend_Controller_Router_Exception $e) {
                $routeName = 'default';
            }
        }
        //requested routename/default

        $route = $router->getRoute($routeName);
        //the current route

        $url = '/' . trim($frontController->getBaseUrl(), '/');
        if($url != '/') {
            $url .= '/';
        }
        $url .= $route->assemble($params, $reset);
        
        $baseUrl = Madoqua_Application::getConfiguration()->url;
        
        $frontController->getResponse()->setRedirect($baseUrl . $url);
        $frontController->getResponse()->sendHeaders();
        //send the headers of the response
        
        exit();
        //STOP! Dik-dik police!
    }
}